//
//  FMImageViewController.m
//  WeEsion
//
//  Created by reylen on 16/1/26.
//  Copyright © 2016年 fdauto. All rights reserved.
//

#import "FMImageViewController.h"

@interface FMImageViewController ()<UIScrollViewDelegate>{
    CGFloat screenWidth;
    NSInteger selectedIndex;
    BOOL startDrag;
}

@property (retain, nonatomic) UIImageView *imageViewL;
@property (retain, nonatomic) UIImageView *imageViewC;
@property (retain, nonatomic) UIImageView *imageViewR;

@property (retain, nonatomic) UILabel *descriptionLabel;


@property (retain, nonatomic) UIScrollView* scrollView;

@property (retain, nonatomic) NSArray* imagesArray;
@property (copy, nonatomic) NSString* basePath;

@end

@implementation FMImageViewController

- (instancetype)initWithImages:(NSArray *) images basePath:(NSString *) basePath index:(NSInteger) index
{
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        
        selectedIndex = index;
        self.imagesArray = images;
        self.basePath = basePath;
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    [self initUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) initUI {
    
    screenWidth = [UIScreen mainScreen].bounds.size.width;
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.scrollView];
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.contentSize = CGSizeMake(screenWidth*3, 0);
    
    CGRect frame = self.view.bounds;
    self.imageViewL = [[UIImageView alloc] initWithFrame:frame];
    self.imageViewL.contentMode = UIViewContentModeCenter;
//    self.imageViewL.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:.5];
    
    frame.origin.x = screenWidth;
    self.imageViewC = [[UIImageView alloc] initWithFrame:frame];
    self.imageViewC.contentMode = UIViewContentModeCenter;
//    self.imageViewC.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:.5];

    frame.origin.x = screenWidth * 2;
    self.imageViewR = [[UIImageView alloc] initWithFrame:frame];
    self.imageViewR.contentMode = UIViewContentModeCenter;
//    self.imageViewR.backgroundColor = [[UIColor greenColor] colorWithAlphaComponent:.5];

    [self.scrollView addSubview:_imageViewL];
    [self.scrollView addSubview:_imageViewC];
    [self.scrollView addSubview:_imageViewR];
    
    [self.scrollView setContentOffset:CGPointMake(screenWidth, 0)];
    
    if (_imagesArray.count <= 1) {
        self.scrollView.scrollEnabled = NO;
    }
    
    self.descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 35, screenWidth, 20)];
    self.descriptionLabel.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    _descriptionLabel.textAlignment = NSTextAlignmentCenter;
    _descriptionLabel.textColor = [UIColor whiteColor];
    _descriptionLabel.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:self.descriptionLabel];
    
    [self setInShowImageWithSelectedIndex:selectedIndex];
}

- (void) setInShowImageWithSelectedIndex:(NSInteger) index {
    
//    selectedIndex = index;
    
    NSInteger iCount = _imagesArray.count;
//    if (index == -1) {
//        index = iCount - 1;
//    }
//    else
//    {
//        index = index % iCount;
//    }
    
    NSInteger pre = index - 1;
    if (pre == -1) {
        pre = iCount - 1;
    }
    NSInteger next = index + 1;
    if (next == iCount) {
        next = 0;
    }
    
    NSString* imageNameL = [_imagesArray objectAtIndex:pre];
    NSString* imageNameC = [_imagesArray objectAtIndex:index];
    NSString* imageNameR = [_imagesArray objectAtIndex:next];
    
    UIImage* imageL = [self imageWithImageName:imageNameL];
    UIImage* imageC = [self imageWithImageName:imageNameC];
    UIImage* imageR = [self imageWithImageName:imageNameR];
    
    self.title = [_imagesArray objectAtIndex:index];
    
    self.descriptionLabel.text = [NSString stringWithFormat:@"%i/%i",(int)selectedIndex+1,(int)iCount];
    
    self.imageViewL.image = imageL;
    self.imageViewC.image = imageC;
    self.imageViewR.image = imageR;
    
//    [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    self.scrollView.bounds = CGRectMake(screenWidth, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height);
    
    NSLog(@"%@",NSStringFromCGPoint(self.scrollView.contentOffset));
}

- (UIImage *) imageWithImageName:(NSString *) imageName {
    NSString* filePath = [self.basePath stringByAppendingPathComponent:imageName];
    UIImage* image = [UIImage imageWithContentsOfFile:filePath];
    
    return image;
}

#pragma mark -

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger off_x = scrollView.contentOffset.x;
    NSInteger page = off_x / screenWidth;
    NSInteger x = off_x % (NSInteger)screenWidth;
    
    if (x > screenWidth /2) {
        page ++;
    }
    
    if (page == 0) {
        selectedIndex -= 1;
    }
    else if (page == 2){
        selectedIndex += 1;
    }
    
    if (selectedIndex < 0) {
        selectedIndex = _imagesArray.count - 1;
    }
    else if (selectedIndex == _imagesArray.count){
        selectedIndex = 0;
    }
    
    [self setInShowImageWithSelectedIndex:selectedIndex];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    

}

@end
